# coding:utf-8
from functools import wraps
import logging


def logged(level, name=None, message=None):
    def decorate(func):
        logname = name if name else func.__module__
        log = logging.getLogger(logname)
        logmsg = message if message else func.__name__

        @wraps(func)
        def wrapper(*args, **kwargs):
            log.log(level, logmsg)
            return func(*args, **kwargs)
        return wrapper
    return decorate

# 带参数的装饰器


def preDecotor(message):
    print(message)

    def decotor(func):
        def inner(*args, **kwargs):
            print("It is Inner Function")
            return func(*args, **kwargs)
            pass
        return inner
    return decotor


@preDecotor("add函数")
@logged(logging.DEBUG)
def add(x, y):
    return x + y


@logged(logging.CRITICAL,  'example')
def spam():
    print("Spam!")


if __name__ == "__main__":
    # 设置日志输出的文件，输出的格式
    logTest = logging.getLogger("test")
    logTest.info("info")
    logTest.warning("warn")
    import sys
    sys.exit()
    logging.basicConfig(format="%(asctime)s:%(levelname)s:%(message)s",
                        datefmt="%m-%d-%Y %H:%M:%S",
                        filename=r".\example.log", level=logging.INFO)
    infoLog = logging.getLogger("tcpServer")
    infoLog.debug("debug")
    infoLog.info("info")
    infoLog.warning("warning ")
